﻿using Common;
using NB.MGJ.Common;
using NB.MGJ.Common.API;
using NB.MGJ.Common.Cache;
using NB.MGJ.Common.Helper;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Service;
using NB.MGJ.Service.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace NB.MGJ.API
{
    /// <summary>
    /// 微信-我的
    /// nbapi/weixin/my
    /// </summary>
    [ApiAuthorizeFilter]
    [ApiHandleError]
    public class ApiMGJMyController : ApiBaseController<LogOnUserModel>
    {
        ManagerService ms = new ManagerService();
        ConfigManagerService sys = new ConfigManagerService();

        #region 修改密码
        /// <summary>
        /// -1  原密码错误
        ///  0  修改失败
        ///  大于0  修改成功
        /// </summary>
        [ApiVisitFilter("EditPwd")]
        [Route("nbapi/my/modifypwd")]
        [HttpPost]
        public IHttpActionResult EditPwd([FromBody]dynamic obj)
        {
            try
            {
                int userid = Convert.ToInt32(obj.userid);
                string newpwd = Convert.ToString(obj.newpwd);
                Sys_User user = ms.GetUserInfoById(userid);
                user.Pwd = EDS3.DESEnCode(newpwd);
                return ApiSuccess(ms.EditUserInfoService(user));
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        #endregion

        #region 获取用户信息
          [ApiVisitFilter("privateInfo")]
        [Route("nbapi/my/getuser")]
        [HttpGet]
        public IHttpActionResult GetUserInfo(int? userid)
        {
            return ApiSuccess(ms.GetAppUserInfoById(userid.Value));
        }
        #endregion

        #region 绑定手机号
        [Route("nbapi/my/sendsms")]
        [HttpGet]
        public IHttpActionResult SendSMS(string mobile, string name)
        {
            try
            {
                SMSApi smsApi = new SMSApi();
                string str = smsApi.SendSMSApi("81a498a753448f368a5ad26555c165bc", mobile, "41978", name);
                return ApiSuccess(new { code = str });
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        [Route("nbapi/my/updatephone")]
        [HttpGet]
        public IHttpActionResult UpdateMobile(int userid, string mobile)
        {
            try
            {
                Sys_User user = ms.GetUserInfoById(userid);
                user.Modile = mobile;
                int r = ms.EditUserInfoService(user);
                return ApiSuccess(r > 0 ? "绑定成功！" : "绑定失败！");
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        #endregion

        #region 修改用户信息
        [ApiVisitFilter("PrivateInfo")]
        [Route("nbapi/my/edituser")]
        [HttpPost]
        public IHttpActionResult EditUser([FromBody]AppUserModel model)
        {
            try
            {
                Sys_User user = ms.GetUserInfoById(model.ID);
                user.RealName = model.RealName;
                user.UserImg = model.UserImg;
                return ApiSuccess(ms.EditUserInfoService(user) > 0 ? "修改成功！" : "修改失败！");
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        #endregion

        #region 绑定微信
        [Route("nbapi/my/bindwx")]
        [HttpPost]
        public IHttpActionResult bindWX([FromBody]dynamic obj)
        {
            try
            {
                string openid = Convert.ToString(obj.openid);
                int userid = Convert.ToInt32(obj.userid);
                Sys_User user = ms.GetUserInfoById(userid);
                user.WXID = openid;
                int r = ms.EditUserInfoService(user);
                return ApiSuccess(r > 0 ? "绑定成功！" : "绑定失败！");
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        /// <summary>
        /// 这个地方写解密的方法
        /// </summary>
        /// <param name="encryptedData"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        //public string getOpenId(string encryptedData, string iv)
        //{
        //    return "我是OPENID";
        //}
        #endregion


    }
}
